package com.bole.controller;


import com.bole.dao.UserDao;
import com.bole.data.Result;
import com.bole.data.ResultEnum;
import com.bole.dto.user.UserDto;
import com.bole.pojo.TbUserDo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.List;

@RestController
@RequestMapping("user")
@Api(value="会员管理接口")
public class UserController {
    @Autowired
    private UserDao userDao;

    @GetMapping("getUserInfo")
    @ApiOperation(value = "查询会员信息")
    public List<UserDto> getUserInfo() {
        List<TbUserDo> all = userDao.findAll();
        ArrayList<UserDto> dicContentTypeDtoArrayList = new ArrayList<>();
        for (TbUserDo userDo : all) {
            UserDto userDto = new UserDto();
            BeanUtils.copyProperties(userDo, userDto);
            dicContentTypeDtoArrayList.add(userDto);
        }
        return dicContentTypeDtoArrayList;

    }


    @PostMapping("toUpdate")
    @ApiOperation(value = "回显")
    @ApiImplicitParam(name = "id",value = "会员id",required = true,dataTypeClass = Long.class)
    public UserDto toUpdate(@RequestParam("id") Long id) {
        TbUserDo  userDo = userDao.findById(id).get();
        UserDto userDto = new UserDto();
        BeanUtils.copyProperties(userDo, userDto);
        return userDto;
    }

    @PostMapping("updateUserInfo")
    @ApiOperation(value = "修改会员信息")
    @ApiImplicitParam(name = "userDo",value = "会员对象",required = true,dataTypeClass = TbUserDo.class)
    public void updateUserInfo(@RequestBody TbUserDo userDo) {
        TbUserDo  tbUserDo = userDao.save(userDo);
        UserDto userDto = new UserDto();
        BeanUtils.copyProperties(tbUserDo ,userDto);
    }




}
